package com.youyou.fusheng2.View.Fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.HomeTedayTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Adapter.ViewPagerItemAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name fusheng2
 * @class name：com.youyou.fusheng2.View.Fragment
 * @anthor 浮生 QQ:2567294605
 * @time 2020/8/27 03:11
 * @to
 */
public class PublicFragment extends Fragment implements FuShengCallBack, OnLoadMoreListener, OnRefreshListener, OnClick2 {

    private int                       id;
    private FuShengMoudle             fuShengMoudle = new FuShengMoudle(this);
    private MyHander                  myHander      = new MyHander();
    private List<Map<String, String>> ImageItem     = new ArrayList<>();
    private Map<String, String>       mapItem;
    private SmartRefreshLayout        home_Refresh;
    private ViewPagerItemAdapter      viewPagerItemAdapter;
    private int                       s             = 1;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.home_viewpager_item, container, false);
        GetShuju(s);
        setDate(inflate);
        return inflate;
    }

    private void setDate(View inflate) {
        home_Refresh = inflate.findViewById(R.id.home_Refresh);
        home_Refresh.setOnLoadMoreListener(this);
        home_Refresh.setOnRefreshListener(this);
        home_Refresh.autoRefresh();
        viewPagerItemAdapter = new ViewPagerItemAdapter(this);
        RecyclerView home_recyclerView = inflate.findViewById(R.id.home_recyclerView);
        home_recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3, RecyclerView.VERTICAL, false));
        home_recyclerView.setAdapter(viewPagerItemAdapter);
        viewPagerItemAdapter.setMapList(ImageItem);
        viewPagerItemAdapter.notifyDataSetChanged();
    }

    private void GetShuju(int s) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  18:26
         *  @简介:
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (id == 0) {
                    fuShengMoudle.getUrlDate(EveryUrl.vdioType3 + 5 + "/" + s, 0);
                } else if (id == 1) {
                    fuShengMoudle.getUrlDate(EveryUrl.vdioType3 + id + "/" + s, 0);
                } else if (id == 2) {
                    fuShengMoudle.getUrlDate(EveryUrl.vdioType3 + 34 + "/" + s, 0);
                } else {
                    fuShengMoudle.getUrlDate(EveryUrl.vdioType3 + 4 + "/" + s, 0);
                }
            }
        }).start();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (getArguments() != null) {
            id = getArguments().getInt("string");
        }
    }

    @Override
    public void GetSuccess(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求成功
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求成功
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        GetShuju(this.s += 18);
        home_Refresh.finishLoadMore(2000);
        viewPagerItemAdapter.setMapList(ImageItem);
        viewPagerItemAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        home_Refresh.finishRefresh(1000);
        viewPagerItemAdapter.notifyDataSetChanged();
    }

    @Override
    public void listener1(int Flage, String Text) {
        Bundle bundle = new Bundle();
        bundle.putInt("f", Integer.parseInt(Text));
        getContext().startActivity(new Intent(getContext(), com.youyou.fusheng2.View.Activity.playVdioActivity.class).putExtra("text", bundle));
    }

    @Override
    public void listener1(int Flage, String Text, int Index) {

    }

    @SuppressLint("HandlerLeak")
    public class MyHander extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    jiexi(msg.obj.toString());
                    break;
            }
        }
    }

    private void jiexi(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:37
         *  @简介: 解析home页面的底部每个item
         */
        Log.i("LikeItem", "setdata: +" + toString);
        Gson gson = new Gson();
        HomeTedayTest homeTedayTest = gson.fromJson(toString, HomeTedayTest.class);
        if (homeTedayTest.getCode() == 0) {
            List<HomeTedayTest.DataBean> data = homeTedayTest.getData();
            for (int i = 0; i < data.size(); i++) {
                mapItem = new HashMap<>();
                mapItem.put("tid", String.valueOf(data.get(i).getV_id()));
                mapItem.put("tname", data.get(i).getV_name());
                mapItem.put("tpic", data.get(i).getV_pic());
                mapItem.put("tnote", String.valueOf(data.get(i).getV_score()));
                ImageItem.add(mapItem);
            }
        }
    }
}
